package com.lsfb.testgamedemo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.loopj.android.http.RequestParams;
import com.lsfb.testgamedemo.Utils.AsynHttp;
import com.lsfb.testgamedemo.Utils.CommonValue;
import com.lsfb.testgamedemo.Utils.LittleUtils;
import com.lsfb.testgamedemo.Utils.NetCallBack;
import com.lsfb.testgamedemo.Utils.SPUtils;
import com.lsfb.testgamedemo.Utils.URLString;
import com.lsfbandroid.helloplugin.R;

import org.xutils.common.util.MD5;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.Enumeration;

import cn.jpush.android.api.JPushInterface;



@ContentView(R.layout.activity_main2)
public class Main2Activity extends Activity implements View.OnClickListener {

    @ViewInject(R.id.tv1)
    private TextView textView;

    @ViewInject(R.id.edt1)
    private EditText edt1;

    @ViewInject(R.id.edt2)
    private EditText edt2;

    @ViewInject(R.id.edt3)
    private EditText edt3;

    @ViewInject(R.id.loginlayout)
    private LinearLayout login_layout;

    @ViewInject(R.id.verylayout)
    private LinearLayout verylayout;

    @ViewInject(R.id.mingling_pwd)
    private TextView tv_ml;

    private String szImei;

    private String TAG = "Nonsense";

    @ViewInject(R.id.btn1)
    private Button btn1;

    @ViewInject(R.id.btn2)
    private Button btn2;

    @ViewInject(R.id.btn3)
    private Button btn3;

    @ViewInject(R.id.register)
    private TextView btn_register;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        Update();
        verylayout.setVisibility(View.GONE);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn_register.setOnClickListener(this);
        TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        szImei = TelephonyMgr.getDeviceId();
        Log.e(TAG, "Deviceid:" + MD5.md5(szImei));
        textView.setText(MD5.md5(szImei));
        String ml = (String) SPUtils.get(getApplication(), "password", "123");
        if (ml != null) {
            tv_ml.setText(ml);
        }

    }


    private String login_url = URLString.CHECKING;
    private String Update_url = URLString.UPDATE;


    public void SuccessLogin() {
        App.Islogin = true;
        startActivity(new Intent(this, Main3Activity.class));
        this.finish();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn1:
                Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
                startActivity(intent);
                break;
            case R.id.btn2:
                //登陆
                if (edt1.getText().toString().isEmpty() || edt2.getText().toString().isEmpty()) {
                    Toast.makeText(x.app(), "用户或者密码不能为空", Toast.LENGTH_LONG).show();
                    return;
                }
                Login(edt1.getText().toString(), edt2.getText().toString(), szImei);
//                SuccessLogin();
                break;
            case R.id.btn3:
                //修改验证密码
                if (!edt3.getText().toString().isEmpty()) {
                    SPUtils.remove(getApplication(), "password");
                    SPUtils.put(getApplication(), "password", edt3.getText().toString());
                    tv_ml.setText(edt3.getText().toString());
                    edt3.setText("");
                }
                break;
            case R.id.register:
                Intent intent1 = new Intent();
                intent1.setAction("android.intent.action.VIEW");
                Uri content_url = Uri.parse(URLString.REGISTER);
                intent1.setData(content_url);
                startActivity(intent1);
                break;
        }
    }

    public void Login(String username, String password, String tzcode) {
        RequestParams requestParams = new RequestParams();
        requestParams.add("username", username);
        requestParams.add("password", MD5.md5(password));
        requestParams.add("signature", MD5.md5(tzcode));
        requestParams.add("version", "2.0");

        requestParams.add("rid", JPushInterface.getRegistrationID(this));
        Log.e(TAG, "requestparams:" + requestParams.toString());
//        Log.e(TAG, "password:" + MD5.md5(password));
        new AsynHttp().sendPost(login_url, requestParams, new NetCallBack() {
            @Override
            public void Success(String arg) {
                Log.e(TAG, arg);
                LoginBean bean = new Gson().fromJson(arg, LoginBean.class);
                Toast.makeText(x.app(), bean.getMsg(), Toast.LENGTH_LONG).show();
                if (bean.getCode() == 200) {
                    String[] tmpdata = bean.getData().split("UID:");
                    Log.e(TAG, tmpdata.toString());
                    if (tmpdata.length != 0) {

                        CommonValue.uid = tmpdata[1];
                        CommonValue.endtime = tmpdata[0];
                        SuccessLogin();
                    } else {
                        Toast.makeText(x.app(), "验证失败", Toast.LENGTH_LONG).show();
                    }

                }
            }

            @Override
            public void Failure(Throwable arg) {
                Toast.makeText(x.app(), "没有网络", Toast.LENGTH_LONG).show();
            }
        });
    }

    public void Update() {
        RequestParams requestParams = new RequestParams();
        new AsynHttp().sendPost(Update_url, requestParams, new NetCallBack() {
            @Override
            public void Success(String arg) {
                Log.e(TAG, arg);
                UpdateBean bean = new Gson().fromJson(arg, UpdateBean.class);
                if (bean.getCode() == 200) {
                    try {
                        if (Integer.valueOf(bean.getData().getVersion()) > getVersionCode(Main2Activity.this)) {
                            LittleUtils.showUpdataDialog(Main2Activity.this, bean.getData().getAddress(), Integer.parseInt(bean.getData().getType()));
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        Toast.makeText(x.app(), "版本获取异常", Toast.LENGTH_LONG).show();
                    }
                } else {
                    Toast.makeText(x.app(), "服务器连接失败", Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void Failure(Throwable arg) {
                Toast.makeText(x.app(), "没有网络", Toast.LENGTH_LONG).show();
            }
        });
    }

    public static int getVersionCode(Context context) throws Exception {
        //getPackageName()是你当前类的包名，0代表是获取版本信息
        PackageManager packageManager = context.getPackageManager();
        PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(),
                0);
        return packInfo.versionCode;
    }
}
